Code
rand_ds <- data.frame(x = rnorm(1000, mean = 10, sd = 1),
y = rnorm(1000, mean = 5, sd = 2))
rand_dsrnorm(_) functionrand_ds <- data.frame(x = rnorm(1000, mean = 10, sd = 1),
y = rnorm(1000, mean = 5, sd = 2))
rand_dsplot(_) functionplot(x = rand_ds$x, y = rand_ds$y)ggplot2:: packageggplot2 were initially developed independently, but later harmonised with tidyverse packageslibrary(ggplot2)
ggplot(rand_ds, aes(x, y)) + geom_point()In this practical, we will use data asthmads_clean.sav from dataset folder
library(tidyverse)── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.4
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ lubridate 1.9.3 ✔ tibble 3.2.1
✔ purrr 1.0.2 ✔ tidyr 1.3.0
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(haven)
asthmads_clean <- read_sav("asthmads_clean.sav") %>%
as_factor() %>%
mutate(Wt_Diff = Weight_Post - Weight_Pre)
asthmads_cleanggplot(_), defining a plot object that you then add layers to.ggplot(data = asthmads_clean)asthmads_clean %>%
ggplot(data = .)ggplot(_) how the information from our data will be visually represented.aes(_) function,aes(_) specify which variables to map to the x and y axes.asthmads_clean %>%
ggplot(data = .,
mapping = aes(x = PA_HW,
y = Wt_Diff))geom_geom_point(_) functionasthmads_clean %>%
ggplot(data = .,
mapping = aes(x = PA_HW,
y = Wt_Diff)) +
geom_point()asthmads_clean %>%
ggplot(data = .,
mapping = aes(x = PA_HW,
y = Wt_Diff,
colour = Gender)) +
geom_point()asthmads_clean %>%
ggplot(aes(Gender)) +
geom_bar()asthmads_clean %>%
ggplot(aes(x = Gender, fill = Gender)) +
geom_bar()asthmads_clean %>%
ggplot(aes(Weight_Pre)) +
geom_histogram()`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
asthmads_clean %>%
ggplot(aes(x = Weight_Pre)) +
geom_histogram(fill = "white", colour = "black")`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
asthmads_clean %>%
ggplot(aes(x = Weight_Pre)) +
geom_histogram(fill = "white", colour = "black", binwidth = 2)time_ds <- tibble(time = 1:10,
value = c(2, 3, 5, 7, 8, 9, 10, 12, 14, 15))
time_dstime_ds %>%
ggplot(aes(x = time, y = value)) +
geom_line()asthmads_clean %>%
select(idR, Gender, Weight_Pre, Weight_Post) %>%
pivot_longer(cols = starts_with("Weight"),
names_to = "event",
values_to = "weight")geom_point(_)asthmads_clean %>%
select(idR, Gender, Weight_Pre, Weight_Post) %>%
pivot_longer(cols = starts_with("Weight"),
names_to = "event",
values_to = "weight") %>%
ggplot(aes(x = event,
y = weight)) +
geom_point() +
geom_line(aes(group = idR))asthmads_clean %>%
select(idR, Gender, Weight_Pre, Weight_Post) %>%
pivot_longer(cols = starts_with("Weight"),
names_to = "event",
values_to = "weight") %>%
ggplot(aes(x = event,
y = weight,
colour = Gender)) +
geom_point() +
geom_line(aes(group = idR))nhms19_adm <- read_csv("../dataset/nhms19_adm.csv")Rows: 16 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (1): State
dbl (3): Prevalence, Upper_CI, Lower_CI
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
nhms19_admdownload.file(
url = "https://raw.githubusercontent.com/dosm-malaysia/data-open/main/datasets/geodata/administrative_1_state.geojson",
destfile = "administrative_1_state.geojson",
mode = "wb")sf packagelibrary(sf)Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
my_state_sf <- read_sf("administrative_1_state.geojson")
my_state_sfleft_join(nhms19_adm, my_state_sf)my_state_sf <- my_state_sf %>%
rename(State = state)
my_state_sffull_join(nhms19_adm, my_state_sf)Joining with `by = join_by(State)`
my_state_sf <- my_state_sf %>%
mutate(State = fct_recode(State,
"WP Kuala Lumpur" = "W.P. Kuala Lumpur",
"WP Putrajaya" = "W.P. Putrajaya",
"WP Labuan" = "W.P. Labuan"))
nhms19_adm_m <- full_join(nhms19_adm, my_state_sf)Joining with `by = join_by(State)`
nhms19_adm_mgeom_sf(_) functionnhms19_adm_m %>%
ggplot(aes(fill = Prevalence)) +
geom_sf()nhms19_adm_sf <- st_as_sf(nhms19_adm_m)
nhms19_adm_sfgeom_sf(_) functionnhms19_adm_sf %>%
ggplot(aes(fill = Prevalence)) +
geom_sf()nhms19_adm_sf %>%
ggplot(aes(fill = Prevalence)) +
geom_sf() +
scale_fill_gradient(low = "green", high = "red")nhms19_adm_sf %>%
ggplot(aes(fill = Prevalence)) +
geom_sf() +
scale_fill_gradient(low = "green", high = "red") +
theme_bw() +
theme(axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
panel.border = element_blank())